package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAffiliatedUnitsRel;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAffiliatedUnitsRelService;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelStatusDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * <p>Description：挂靠状态停用MQ消息接收处理类</p>
 *
 * @author panhj
 * @version 1.0
 * @createDate 2020-07-01
 */
@AllArgsConstructor
@Slf4j
@Component
public class AffiliationRelStatusProcessor implements AfsMqBizProcessor<AffiliationRelStatusDto> {

    private final ChannelAffiliatedUnitsRelService channelAffiliatedUnitsRelService;

    @Override
    public boolean processMessage(AffiliationRelStatusDto entity) throws Exception {
        this.AffiliationStatusQueue(entity);
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_UPDATE_REL;
    }

    @Transactional(rollbackFor = Exception.class)
    public String AffiliationStatusQueue(AffiliationRelStatusDto affiliationRelStatusDto) {
        String errorMsg = "";
        try {
            log.info("开始同步挂靠状态信息……", affiliationRelStatusDto);

            ArrayList<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsArrayList = this.channelAffiliatedUnitsArrayList(affiliationRelStatusDto);
            if (channelAffiliatedUnitsArrayList != null && channelAffiliatedUnitsArrayList.size() > 0) {
                channelAffiliatedUnitsRelService.saveOrUpdateBatch(channelAffiliatedUnitsArrayList, channelAffiliatedUnitsArrayList.size());
//                for(int i=0;i<channelAffiliatedUnitsArrayList.size();i++){
//                    List<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsRel=channelAffiliatedUnitsRelService.list(Wrappers.<ChannelAffiliatedUnitsRel>query().lambda()
//                    .eq(ChannelAffiliatedUnitsRel::getArriliatedId,channelAffiliatedUnitsArrayList.get(i).getId()));
//                    for(int j=0;j<channelAffiliatedUnitsRel.size();j++){
//                        channelAffiliatedUnitsRel.get(j).setStatus(AffiiationEnum.STOP.getCode());
//                        channelAffiliatedUnitsRelService.updateById(channelAffiliatedUnitsRel.get(j));
//                    }
//                }
            }
            log.info("########开始同步挂靠状态信息########", affiliationRelStatusDto);

        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error("同步挂靠状态信息失败", affiliationRelStatusDto, e);
        }
        return errorMsg;
    }

    /**
     * 解析挂靠信息
     */
    private ArrayList<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsArrayList(AffiliationRelStatusDto affiliationRelStatusDto) {
        ArrayList<ChannelAffiliatedUnitsRel> channelAffiliatedUnitsArrayList = new ArrayList<>();
        if (affiliationRelStatusDto.getAffiliationRelDtoArrayList() != null) {
            affiliationRelStatusDto.getAffiliationRelDtoArrayList().forEach(AffiliationRelDto -> {
                ChannelAffiliatedUnitsRel channelAffiliatedUnitsRel = channelAffiliatedUnitsRelService.getById(AffiliationRelDto.getId());

//                        (Wrappers.<ChannelAffiliatedUnits>query().lambda()
//                        .eq(ChannelAffiliatedUnits::getId,AffiliationRelDto.getId()));
                if (channelAffiliatedUnitsRel == null) {
                    channelAffiliatedUnitsRel = new ChannelAffiliatedUnitsRel();
                }
                BeanUtil.copyProperties(AffiliationRelDto, channelAffiliatedUnitsRel, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
                channelAffiliatedUnitsArrayList.add(channelAffiliatedUnitsRel);
            });
        }
        return channelAffiliatedUnitsArrayList;
    }

}
